module AccessGroups
  protected

  #----------------------------------------------------------------------------- 
  # initializing
  #----------------------------------------------------------------------------- 
  
  def init_group_users
    @users = User.find(:all, :conditions => "role_id = 2", :order => "last_name")
  end

  def init_group_system_objects
    if current_user_is_site_admin?
      @system_objects = SystemObject.find_managed
    else
      @system_objects = []
      current_user_role.system_object.each do |curso|
        @system_objects << curso if curso.can_remove
      end
    end
  end

  def init_group_folders
    @folders = Folder.list_for_selection
  end

  def init_group_pages
    @pages = Page.list_by_folder
  end

  def init_group_document_categories
    @document_categories = DocumentCategory.find(:all, :order => "name")
  end

  #----------------------------------------------------------------------------- 
  # updating
  #----------------------------------------------------------------------------- 
  
  def update_group_users(group)
    users  = group.user.dup
    users.each do |usr|
      group.user.delete(usr) unless param?("group_user_#{usr.id}")
    end
    params.each do |key,value|
      if key =~ /^group_user_/
        unless group.has_user_id?(get_id_from_param(key))
          group.user << User.find(get_id_from_param(key))
        end
      end
    end
  end

  def update_group_system_objects(group)
    sobjects = group.managed_system_objects
    sobjects.each do |system_object|
      group.system_object.delete(system_object) unless param?("system_object_#{system_object.id}")
    end
    params.each do |key,value|
      if key =~ /^system_object_/
        unless group.has_system_object_id?(get_id_from_param(key))
          group.system_object << SystemObject.find(get_id_from_param(key))
        end
      end
    end
  end

  def update_group_folders(group)
    folders = group.folder.dup
    folders.each do |fldr|
      unless param?("folder_#{fldr.id}")
        FolderContent.all_pages_for(fldr.id) do |pg|
          if group.has_page_id?(pg.id)
            group.page.delete(pg)
            pg.document_category.each do |dc|
              group.document_category.delete(dc) if group.has_document_category_id?(dc.id)
            end
          end
        end
        group.folder.delete(fldr)
      end
    end
    params.each do |key,value|
      if key =~ /^folder_/
        fid = get_id_from_param(key)
        unless group.has_folder_id?(fid)
          FolderContent.all_pages_for(fid) do |pg|
            unless group.has_page_id?(pg.id)
              group.page << pg
              pg.document_category.each do |dc|
                group.document_category << dc unless group.has_document_category_id?(dc.id)
              end
            end
          end
          group.folder << Folder.find(fid)
        end
      end
    end
  end

  def update_group_pages(group)
    pages = group.page.dup
    pages.each do |pg|
      group.page.delete(pg) unless param?("page_#{pg.id}")
    end
    params.each do |key,value|
      if key =~ /^page_/
        unless group.has_page_id?(get_id_from_param(key))
          group.page << Page.find(get_id_from_param(key))
        end
      end
    end
  end

  def update_group_document_categories(group)
    categories = group.document_category.dup
    categories.each do |dc|
      group.document_category.delete(dc) unless param?("document_category_#{dc.id}")
    end
    params.each do |key,value|
      if key =~ /^document_category_/
        unless group.has_document_category_id?(get_id_from_param(key))
          group.document_category << DocumentCategory.find(get_id_from_param(key))
        end
      end
    end
  end
                 

end
